package com.sikao.app.fragment;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Toast;

import com.sikao.app.R;
import com.sikao.app.adapter.MyEntrustsAdapter;
import com.sikao.app.bean.Bean;
import com.sikao.app.bean.DeleteSubmitBean;
import com.sikao.app.bean.EntrustListBean;
import com.sikao.app.bean.GetListSubmitBean;
import com.sikao.app.dao.MyEntrustDeleteModel;
import com.sikao.app.dao.MyEntrustsModel;
import com.sikao.app.fragment.base.MyBaseFragment;
import com.sikao.app.myinterface.Callback;
import com.sikao.app.net.HttpApi;
import com.sikao.app.net.OnNetBackListener;
import com.sikao.app.ui.EntrustDetailsActivity;
import com.sikao.app.utils.JSONUtil;
import com.sikao.app.view.RefreshListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的委托列表
 */
public class MyEntrustsFragment extends MyBaseFragment implements Callback, View.OnClickListener {

	private RefreshListView refreshlistview;
	private MyEntrustsAdapter adapter;
	private MyEntrustsModel myEntrustsModel;
	private MyEntrustDeleteModel myEntrustDeleteModel;
	private GetListSubmitBean getListSubmitBean;
	private DeleteSubmitBean deleteSubmitBean;
	private List<EntrustListBean.ResBean.ListBean> beans;
	private AlertDialog dialog;
	private View rootView;
	private int page = 1;
	private int allPage;
	//0正常加载，1刷新,2加载更多
	private int type;
	private int from;//0代表供应，1代表需求
	private int position;//需要删除的位置
	private View noData, noNetwork;

	@Override
	public void notice(final int position) {
		this.position = position;
		if (myEntrustDeleteModel == null) {
			deleteSubmitBean = new DeleteSubmitBean();
			myEntrustDeleteModel = new MyEntrustDeleteModel(getActivity(), new OnNetBackListener<Bean>() {
				@Override
				public void netBack(Bean data) {
					if (data == null) {
						return;
					}
					if (data.getStatus().equals("1")) {
						Toast.makeText(getActivity(), "删除成功", Toast.LENGTH_SHORT).show();
						beans.remove(MyEntrustsFragment.this.position);
						adapter.notifyDataSetChanged();
					} else {
						Toast.makeText(getActivity(), data.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}
			});
			createDialog();
		}
		deleteSubmitBean.setId(beans.get(position).getId());
		dialog.show();
	}

	private void createDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setMessage("确认删除吗?");
		builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				myEntrustDeleteModel.request(JSONUtil.toJSON(deleteSubmitBean), true);
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		dialog = builder.create();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
							 Bundle savedInstanceState) {
		// Inflate the layout for this fragment
		rootView = inflater.inflate(R.layout.fragment_entrusts, container, false);
		initData();
		initView(rootView);
		initListener();
		init();
		return rootView;
	}

	private void initData() {
		from = getArguments().getInt("from");
	}

	private void initView(View view) {
		refreshlistview = (RefreshListView) view.findViewById(R.id.refreshlistview);
	}

	private void initListener() {
		refreshlistview.setOnRefreshListener(new RefreshListView.OnRefreshListener() {
			@Override
			public void onRefresh() {
				page = 1;
				type = 1;
				getList(false);
			}

			@Override
			public void onLoadMore() {
				page++;
				type = 2;
				if (page > allPage) {
					Toast.makeText(getActivity(), "无更多内容", Toast.LENGTH_SHORT).show();
					refreshlistview.onLoadMoreComplete();
				} else {
					getList(false);
				}
			}
		});
		refreshlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent();
				intent.setClass(getActivity(), EntrustDetailsActivity.class);
				intent.putExtra(EntrustDetailsActivity.ENTRUST_DETAILS, beans.get(position - 1));
				startActivity(intent);
			}
		});
	}

	private void init() {
		isPrepared = true;
		getListSubmitBean = new GetListSubmitBean();
		beans = new ArrayList<>();
		adapter = new MyEntrustsAdapter(getActivity(), beans, this);
		refreshlistview.setAdapter(adapter);
		myEntrustsModel = new MyEntrustsModel(getActivity(), new OnNetBackListener<EntrustListBean>() {
			@Override
			public void netBack(EntrustListBean data) {
				if (type == 1) {
					refreshlistview.onRefreshComplete();
				}
				if (type == 2) {
					refreshlistview.onLoadMoreComplete();
				}
				//无网络
				if (data == null) {
					//清除数据，数据不为空emptyView不会显示
					if (beans.size() != 0) {
						beans.clear();
						adapter.notifyDataSetChanged();
					}
					//如果无数据布局加载过要隐藏掉
					if (noData != null) {
						noData.setVisibility(View.GONE);
					}
					//判断是否加载过无网络布局，避免重复加载显示
					if (noNetwork == null) {
						noNetwork = getActivity().getLayoutInflater().inflate(R.layout.no_network, null);
						((ViewGroup) refreshlistview.getParent()).addView(noNetwork);
						refreshlistview.setEmptyView(noNetwork);
						Button button = (Button) noNetwork.findViewById(R.id.btn_request);
						button.setOnClickListener(new View.OnClickListener() {
							@Override
							public void onClick(View v) {
								type = 0;
								page = 1;
								getList(true);
							}
						});
					} else {
						noNetwork.setVisibility(View.VISIBLE);
					}
					return;

				}
				if (data.getStatus().equals("1")) {
					mHasLoadedOnce = true;
					allPage = data.getRes().getAllPage();
					if (type != 2 && beans.size() != 0) {
						beans.clear();
					}
					beans.addAll(data.getRes().getList());
					adapter.notifyDataSetChanged();
				} else {
					//无数据
					if (data.getStatus().equals("0")) {
						mHasLoadedOnce = true;
						//清除数据，数据不为空emptyView不会显示
						if (beans.size() != 0) {
							beans.clear();
							adapter.notifyDataSetChanged();
						}
						//如果无网络布局加载过要隐藏掉
						if (noNetwork != null) {
							noNetwork.setVisibility(View.GONE);
						}
						//判断是否加载过无数据布局，避免重复加载显示
						if (noData == null) {
							noData = getActivity().getLayoutInflater().inflate(R.layout.content_empty, null);
							((ViewGroup) refreshlistview.getParent()).addView(noData);
							refreshlistview.setEmptyView(noData);
							noData.setOnClickListener(new View.OnClickListener() {
								@Override
								public void onClick(View v) {
									type = 0;
									page = 1;
									getList(true);
								}
							});
						} else {
							noData.setVisibility(View.VISIBLE);
						}

					} else {
						Toast.makeText(getActivity(), data.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}
			}
		});

		lazyLoad();

	}

	private void getList(boolean isShow) {
		getListSubmitBean.setThisPage(page + "");
		getListSubmitBean.setPageNum(HttpApi.LIST_NUMBER);
		switch (from){
			case 0:
				getListSubmitBean.setType("");
				break;
			case 1:
				getListSubmitBean.setType("sell");
				break;
			case 2:
				getListSubmitBean.setType("buy");
				break;
			case 3:
				getListSubmitBean.setType("imports");
				break;
			case 4:
				getListSubmitBean.setType("allsad");
				break;
		}

		myEntrustsModel.request(JSONUtil.toJSON(getListSubmitBean), isShow);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			default:
				break;
		}
	}

	@Override
	protected void lazyLoad() {

	/*	if (!isPrepared || !isVisible || mHasLoadedOnce) {
			return;
		}*/
		if (!isPrepared || !isVisible) {
			return;
		}

		getList(true);
	}
	@Override
	public void onDestroy() {
		super.onDestroy();
		myEntrustsModel.cancelRequest();
	}
}
